$(function () {
    obj.init();
});

var obj = new JsFun();
function JsFun() {
    return {
        dataGrid: null,
        dom: {
            save: null
        },
        params: {
            logonName: null
        },
        init: function () {
            this.params.logonName = xhTool.getQuery('logonName');

            this.dom.save = window.top.xhTop.getModalConfirmDom();

            this.dataGrid = new xhDataGrid($('#list'), 'Device/getSpareRegister');
            this.dataGrid.parseToolbar().init();
            this.funObj().bindDataGridEvent();

            xhTool.bindDocKeyDown($('#bt_list_search'));
            this.listener();
        },
        listener: function () {
            var $this = this;
            //查询
            $('#bt_list_search').click(function () {
                $this.funObj().searchList();
            });
            //使用设备
            $this.dom.save.unbind('click').click(function () {
                $this.funObj().useSpareDevice();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                bindDataGridEvent: function () {
                    xhExtra.clearLoadSuccess($this.dataGrid);
                },
                searchList: function () {
                    $this.dataGrid.reload({
                        search: {
                            items: xhExtra.getValue($('#list_item')),
                            vague: xhExtra.getValue($('#list_vague'))
                        }
                    })
                },
                useSpareDevice: function () {
                    var registerId = $this.dataGrid.getSelectValue('iUTId');
                    if (!registerId) {
                        window.top.xhTop.showError('请选择设备！');
                        return false;
                    }
                    xhExtra.setBtnLoading($this.dom.save);
                    xhExtra.ajaxRequest(
                        'Device/useSpareRegister',
                        {logonName: $this.params.logonName, registerId: registerId},
                        {check: false},
                        function (result) {
                            xhExtra.setBtnNormal($this.dom.save);
                            if(xhExtra.checkError(result)) return false;
                            window.top.xhTop.showSuccess(result.msg);
                            window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                            window.top.xhTop.closeModal();
                        }
                    );
                }
            }
        }
    }
}